package com.elitel.frame.main.config.serviceaccess;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务访问拦截器
 * @author hi_bj
 * @since 2023-10-17
 */

public class CustomServiceAccessInterceptor implements HandlerInterceptor {
    /**
     * 管理员标识
     */
    private final String SYSTEM_DIALOG = "ADMIN";
    /**
     * 来源
     */
    private final String FROM_DIALOG = "from";
    /**
     * 自定义拦截器
     */
    private final List<HandlerInterceptor> handlerInterceptorList = new ArrayList<>();

    public void addInterceptor(HandlerInterceptor handlerInterceptor) {
        handlerInterceptorList.add(handlerInterceptor);
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //排除后台管理系统的访问请求
        if (SYSTEM_DIALOG.equals(request.getParameter(FROM_DIALOG))) {
            return true;
        }
        for (HandlerInterceptor handlerInterceptor : handlerInterceptorList) {
            if (!handlerInterceptor.preHandle(request, response, handler)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
